﻿using System;
using System.Collections.Generic;
using System.Data;
using UDS.Components;

namespace UDS.Entity
{
    public enum PostilType
    {
        [DisplayName("同意")]
        Approve = 1,

        [DisplayName("拒绝")]
        Reject = 2,

        [DisplayName("完成")]
        Complete = 3,

        [DisplayName("回退")]
        Fallback = 4,
    }

    /// <summary>
    /// 批注, 审批意见
    /// </summary>
    /// <remarks>
    /// The postils were stored in table UDS_Flow_Postil.
    /// </remarks>
    public class DocumentPostil
    {
        #region Properties

        /// <summary>
        /// Get or set the author of the postil.
        /// </summary>
        public string Author
        {
            get;
            set;
        }

        public string Content
        {
            get;
            set;
        }

        public DateTime TimeCreated
        {
            get;
            set;
        }

        public PostilType Type
        {
            get;
            set;
        }

        /// <summary>
        /// Get or set the time used to create this postil in minutes. 
        /// </summary>
        public int TimeConsumed
        {
            get;
            set;
        }

        public DocumentFile File
        {
            get;
            set;
        }

        #endregion

        /// <summary>
        /// 获得文档的批注
        /// </summary>
        public static IList<DocumentPostil> GetDocumentPostils(int docID)
        {
            var db = new Database();
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, docID)
            };

            var list = new List<DocumentPostil>();
            using (var dr = db.RunDataReader("dbo.sproc_Flow_GetDocumentPostil_V2", parameters))
            {
                while (dr.Read())
                {
                    list.Add(ReadPostil(dr));
                }
            }

            return list;
        }

        /// <summary>
        /// 添加批注
        /// </summary>
        /// <param name="userName">批注人</param>
        /// <param name="docId">批注的文档ID</param>
        /// <param name="PostilType">批注类型，通过，拒绝，完成</param>
        internal static int AddPostil(Database db, string userName, int docId, string Postil, PostilType postilType, int ObjID, int ObjType)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, docId),
                Database.MakeInParam("@Postiler", SqlDbType.NVarChar, 300, userName),
                Database.MakeInParam("@PostilContent", SqlDbType.NText, 0, Postil),
                Database.MakeInParam("@PostilType", SqlDbType.Int, 4, (int)postilType),
                Database.MakeInParam("@ObjID", SqlDbType.Int, 4, ObjID),
                Database.MakeInParam("@ObjType", SqlDbType.Int, 4, ObjType)
            };

            int id = db.RunProc("dbo.sproc_Flow_AddPostil_V2", parameters);
            if (id <= 0)
            {
                throw new InvalidOperationException("Failed to create a new postil for the specified document.");
            }

            return id;
        }

        /// <summary>
        /// 删除文档最近的批注
        /// </summary>
        /// <param name="docId">文档ID</param>
        internal static int CancelLatestPostil(int docId)
        {
            var parameters = new[]
            {
                Database.MakeInParam("@DocID", SqlDbType.Int, 4, docId)
            };

            using (var db = new Database())
            {
                return db.RunProc("dbo.sproc_Flow_CancelPostil_V2", parameters);
            }
        }

        private static DocumentPostil ReadPostil(IDataReader dr)
        {
            var p = new DocumentPostil();

            p.Author = Convert.ToString(dr["RealName"]);
            p.Content = Convert.ToString(dr["Postil_Content"]);
            p.TimeCreated = Utility.ReadSQLDate(dr["Postil_Date"]);
            p.Type = (PostilType)Utility.ReadInt32(dr["Postil_Type"], 0);
            p.TimeConsumed = (int)dr["UsedTimes"];
            p.File = DocumentFile.ReadFile(dr, true);

            return p;
        }
    }
}
